// pages/product/product-detail.js
const app = getApp()
const WxParse = require('../../wxParse/wxParse.js')
import apis from '../../config/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    productList: [],
    productDetail: {},
    productId: '',
    managerObj: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      productId: options.gid
    })
    if (options.phone) {
      wx.setStorageSync('phone', options.phone)
      // wx.setStorageSync('phone', options.phone)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    that.getProductDetail(this.data.productId)
    that.getProductList()
    if (wx.getStorageSync('phone')) {
      console.log(1)
      app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
        wx.request({
          url: app.globalData.hostAPI + apis.getPhone + '?access_token=' + accessToken,
          method: 'GET',
          data: {
            type: '2',
            phone: wx.getStorageSync('phone')
          },
          success: res => {
            if (res.data.status === 1) {
              that.setData({
                managerObj: res.data.data
              })
              wx.setStorageSync('manager', that.data.managerObj)
            } else if (res.data.status === -1) {
              wx.removeStorageSync('access_token')
              wx.navigateTo({
                url: '/pages/login/login',
              })
            } else {
              wx.showToast({
                title: res.data.msg
              })
            }
          }
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

    /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (wx.getStorageSync('phone')) {
      return {
        title: this.data.productDetail.goods_name,
        path: '/pages/product/product-detail?gid=' + this.data.productId + '&phone=' + wx.getStorageSync('phone'),
        imageUrl: this.data.productDetail.goods_cover
      }
    } else {
      return {
        title: this.data.productDetail.goods_name,
        imageUrl: this.data.productDetail.goods_cover
      }
    }
  },

  onShareTimeline: function () {
    return {
      title: this.data.productDetail.goods_name,
      imageUrl: this.data.productDetail.goods_cover
    }
  },

  // 获取产品详情
  getProductDetail(id) {
    const that = this
    wx.showLoading()
    wx.request({
      url: app.globalData.hostAPI + apis.productDetail + '?id=' + id,
      method: 'get',
      success: res => {
        WxParse.wxParse('gzyl', 'html', res.data.data.content_gzyl, that, 5);
        WxParse.wxParse('syks', 'html', res.data.data.content_syks, that, 5);
        WxParse.wxParse('syz', 'html', res.data.data.content_syz, that, 5);
        WxParse.wxParse('yqtd', 'html', res.data.data.content_yqtd, that, 5);
        that.setData({
          productDetail: res.data.data
        })
        wx.hideLoading()
      }
    })
  },
  getProductList () {
    const that = this
    wx.request({
      url: app.globalData.hostAPI + apis.productList,
      method: 'GET',
      data: {
        is_hot: 1
      },
      success: res => {
        that.setData({
          productList: res.data.data
        })
      }
    })
  }
})